package cn.v2tila.rsosa.student.controller;

import cn.v2tila.rsosa.student.entity.ContactsEntity;
import cn.v2tila.rsosa.student.entity.MessageEntity;
import cn.v2tila.rsosa.student.entity.MsgTemplateEntity;
import cn.v2tila.rsosa.student.entity.UserEntity;
import cn.v2tila.rsosa.student.service.*;
import cn.v2tila.rsosa.student.utils.utils;
import cn.v2tila.rsosa.student.vo.ContactVO;
import cn.v2tila.rsosa.student.vo.MsgVO;
import cn.v2tila.rsosa.student.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author v2tila on 18:22 2019/4/5
 */
@RestController
@RequestMapping(value = "msg")
public class msgController {
    @Autowired
    private MsgTemplateService msgTemplateService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private ContactsService contactsService;
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private AssoService assoService;

    @RequestMapping(value = "saveTemplate")
    public ResultVO saveTemplate(String uniqueId, String text) {
        ResultVO resultVO = new ResultVO();
        MsgTemplateEntity msgTemplateEntity = new MsgTemplateEntity();
        msgTemplateEntity.setUniqueId(uniqueId);
        msgTemplateEntity.setText(text);
        MsgTemplateEntity isSuccess = msgTemplateService.saveAndFlush(msgTemplateEntity);
        if(isSuccess==null){
            resultVO.setCode(0);
            resultVO.setMsg("请联系管理员");
        }else{
            resultVO.setCode(1);
        }

        return resultVO;
    }
    @RequestMapping(value = "getMsgTemplate")
    public List<MsgTemplateEntity> getMsgTemplate(String uniqueId){
        List<MsgTemplateEntity> msgTemplateEntities = msgTemplateService.findByUniqueId(uniqueId);
        return msgTemplateEntities;
    }

    @RequestMapping(value = "deleteMsgTemplate")
    public ResultVO deleteMsgTemplate(Integer id){
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(1);
        try {
            msgTemplateService.deleteById(id);
        }catch (Exception e){
            resultVO.setCode(0);
        }
        return resultVO;
    }

    @RequestMapping(value = "sendMsg")
    public ResultVO sendMsg(String uniqueId, String contactUniqueId, String text) {
        ResultVO resultVO = new ResultVO();
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setUniqueId(uniqueId);
        messageEntity.setContactUniqueId(contactUniqueId);
        messageEntity.setText(text);
        messageEntity.setCreateTime(new utils().getTime());

        MessageEntity isSuccess = messageService.saveAndFlush(messageEntity);
        if (isSuccess == null) {
            resultVO.setCode(0);
            resultVO.setMsg("请联系管理员");
        } else {

            if(!contactsService.existsByUniqueIdAndContactUniqueId(uniqueId,contactUniqueId)){
                ContactsEntity contactsEntity = new ContactsEntity();
                contactsEntity.setUniqueId(uniqueId);
                contactsEntity.setContactUniqueId(contactUniqueId);
                contactsService.saveAndFlush(contactsEntity);
            }
            if(!contactsService.existsByUniqueIdAndContactUniqueId(contactUniqueId,uniqueId)){
                ContactsEntity contactsEntity = new ContactsEntity();
                contactsEntity.setContactUniqueId(uniqueId);
                contactsEntity.setUniqueId(contactUniqueId);
                contactsService.saveAndFlush(contactsEntity);
            }

            ContactsEntity contactsEntity = contactsService.findByUniqueIdAndContactUniqueId(contactUniqueId, uniqueId);
            contactsEntity.setNotRead(contactsEntity.getNotRead()+1);
            ContactsEntity saveReadSuccess = contactsService.saveAndFlush(contactsEntity);
            if(saveReadSuccess==null){
                resultVO.setCode(0);
                resultVO.setMsg("请联系管理员");
            }else{
                resultVO.setCode(1);
            }
        }
        return resultVO;
    }
    @RequestMapping(value = "getContacts")
    public ResultVO getContacts(String uniqueId){
        ResultVO resultVO = new ResultVO();
        List<ContactsEntity> contactsEntityList = contactsService.findByUniqueId(uniqueId);
        List<ContactVO> contactVOList = new ArrayList<ContactVO>();
        for (int i = 0; i < contactsEntityList.size(); i++) {
            ContactVO contactVO = new ContactVO();
            UserEntity userEntity = userService.findByUniqueId(contactsEntityList.get(i).getContactUniqueId());
            String avatarUrl = userEntity.getAvatarUrl();
            String name = studentService.findByUniqueId(userEntity.getUniqueId()).getName();
            if(assoService.findByUniqueId(userEntity.getUniqueId())!=null){
                String assoName = assoService.findByUniqueId(userEntity.getUniqueId()).getName();
                contactVO.setAssoName(assoName);
            }
            contactVO.setContactUniqueId(contactsEntityList.get(i).getContactUniqueId());
            contactVO.setNotRead(contactsEntityList.get(i).getNotRead());
            contactVO.setAvatarUrl(avatarUrl);
            contactVO.setName(name);
            contactVOList.add(contactVO);
        }
        resultVO.setContactVOList(contactVOList);

        return resultVO;
    }

    @RequestMapping(value = "getMessage")
    public List<MsgVO> getMessage(String uniqueId, String contactUniqueId) {
        List<MessageEntity> messageEntities = messageService.findByUniqueIdAndContactUniqueId(uniqueId, contactUniqueId);
        System.out.println(messageEntities);
        List<MsgVO> msgVOList = new ArrayList<MsgVO>();

        for(int i = 0;i<messageEntities.size();i++){
            MsgVO msgVO = new MsgVO();
            String speaker = messageEntities.get(i).getUniqueId();
            String content = messageEntities.get(i).getText();
            String msgTime = messageEntities.get(i).getCreateTime();
            if(uniqueId.equals(speaker)){
                msgVO.setSpeaker("right");
            }else {
                msgVO.setSpeaker("left");
            }
            msgVO.setMsgTime(msgTime);
            msgVO.setContent(content);
            msgVOList.add(msgVO);
        }
        return msgVOList;
    }

    @RequestMapping(value = "cleanRead")
    public ResultVO cleanRead(String uniqueId, String contactUniqueId) {
        ResultVO resultVO = new ResultVO();
        ContactsEntity contactsEntity = contactsService.findByUniqueIdAndContactUniqueId(uniqueId, contactUniqueId);
        contactsEntity.setNotRead(0);
        ContactsEntity saveIsSuccess = contactsService.saveAndFlush(contactsEntity);
        if (saveIsSuccess == null) {
            resultVO.setCode(0);
            resultVO.setMsg("请联系管理员");
        } else resultVO.setCode(1);
        return resultVO;
    }


}
